/// CreateTime 2020/7/3 12:19
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';



class PageStoragePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Page Test',
      home: new Scaffold(
          appBar: AppBar(title: Text("Home")),
          body: new PageView.builder(
            itemBuilder: (BuildContext context, int index) =>
            new TestPage(index: index, key: new PageStorageKey<String>('page-$index')),
            itemCount: 3,
          )),
    );
  }
}

class TestPage extends StatefulWidget {
  final int index;

  TestPage({Key key, @required this.index}) : super(key: key);

  @override
  TestPageState createState() => new TestPageState();
}

int _globalCounter = 0;

class TestPageState extends State<TestPage> {
  int _counter;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    int value = PageStorage.of(context).readState(context);
    if (value == null) {
      _counter = _globalCounter++;
      PageStorage.of(context).writeState(context, _counter);
    } else {
      _counter = value;
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Center(child: new Text("Page #${widget.index}\nCounter #$_counter"));
  }
}